package com.gezi.admin.security;

import com.gezi.admin.security.filter.AccessInterceptorFilter;
import com.gezi.admin.security.filter.AuthenticationHandler;
import com.gezi.admin.security.filter.JwtAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Author 格子代码
 * @createDate 2024/2/10 09:00
 * @contact 1424529270@qq.com
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 忽略路径
     */
    @Autowired
    private JwtAuthenticationFilter jwtAuthenticationFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 开启跨域
        http.csrf().disable().cors();
        //不通过Session获取SecurityContext
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        //认证。这都都需要认证
        http.authorizeRequests().anyRequest().authenticated();
        //token认证
        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        //认证异常处理
        http.exceptionHandling().authenticationEntryPoint(new AuthenticationHandler());
        //url权限认证处理
        AccessInterceptorFilter accessInterceptorFilter = new AccessInterceptorFilter();
        http.addFilterBefore(accessInterceptorFilter, FilterSecurityInterceptor.class);

        //视频上传
        http.headers().frameOptions().disable();
    }
}
